﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.Common;
using System.Text;
using Model.Filters;
using Model.Query_Model;
using Datasource;

namespace Presenters.Managers
{
    public static class ClientManager
    {
        private static IList<ClientOutput> clientList = new List<ClientOutput>();

        private static IList<ClientOutput> BuildCollection(DbDataReader reader)
        {
            clientList.Clear();
            while (reader.Read())
            {
                clientList.Add(new ClientOutput
                {
                    idClient=reader.GetInt32(0),
                    nameCorporation=reader.GetString(1),
                    nameClient=reader.GetString(2),
                    surnameClient=reader.GetString(3),
                    inventedName=reader.GetString(4),
                    phoneNumber=reader.GetString(5),
                    emailNumber=reader.GetString(6),
                    dateModify=reader.GetDateTime(7),
                    isProtected=reader.GetBoolean(8)
                }
                );
            }
            return clientList;
        }

        public static IList<ClientOutput> SearchClient(ClientCriteria criteria)
        {
            DbConnection connection = DbUtil.GetActivityConnection();
            DbCommand command = new SqlCommand();
            DbUtil.SetConnection(connection, command);
            DbUtil.SetQuery(command, new DbQuery { sql = "exec [dbo].[ClientSearch] @idRoad" });
            DbUtil.SetParametr(command, "idRoad", criteria.idRoad);
            return BuildCollection(DbUtil.ExecuteReader(command));
        }
    }
}
